package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdPatronsData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdPatronsDaoImpl extends TempDataObjectDao<StrongholdPatronsData> {
    public static String getStrongholdPatronsDataSql = "select * from t_u_activity_stronghold_patrons where activityId=? and userId=?";

    public Map<Integer, StrongholdPatronsData> getStrongholdPatronsData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdPatronsData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdPatronsDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdPatronsData strongholdPatronsData = parse(rs);
                dataMap.put(strongholdPatronsData.getPatronsId(), strongholdPatronsData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdPatronsDataSql = "update t_u_activity_stronghold_patrons set freeTimes=?,buyTimes=? where activityId=? and userId=? and patronsId=?";

    @Override
    protected boolean updateData(StrongholdPatronsData strongholdPatronsData) {
        strongholdPatronsData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getBuyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdPatronsData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getPatronsId()));
        if (getDb().executeNoneQuery(updateStrongholdPatronsDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdPatronsDataSql = "insert into t_u_activity_stronghold_patrons(activityId,userId,patronsId,freeTimes,buyTimes) values (?,?,?,?,?)";

    @Override
    protected boolean addData(StrongholdPatronsData strongholdPatronsData) {
        strongholdPatronsData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdPatronsData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdPatronsData.getBuyTimes()));
        if (getDb().executeNoneQuery(insertStrongholdPatronsDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdPatronsData parseData(ResultSet rs) throws SQLException {
        StrongholdPatronsData strongholdPatronsData = new StrongholdPatronsData();
        strongholdPatronsData.setActivityId(rs.getInt("activityId"));
        strongholdPatronsData.setUserId(rs.getLong("userId"));
        strongholdPatronsData.setPatronsId(rs.getInt("patronsId"));
        strongholdPatronsData.setFreeTimes(rs.getInt("freeTimes"));
        strongholdPatronsData.setBuyTimes(rs.getInt("buyTimes"));
        return strongholdPatronsData;
    }
}
